﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45Tool.Service;
using nblTool.utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels.Controls.DataControls
{
    public class PlanSetViewModel:ViewModelBase
    {
        private GWCmdMakeUpPlanService gWCmdMakeUpPlanService = new GWCmdMakeUpPlanService();
        private List<GWCmdMakeUp> gWCmdMakeUps = new List<GWCmdMakeUp>();
        private GWCmdMakeUp gWCmdMakeUpSelected;

        public PlanSetViewModel()
        {

        } 

        private ObservableCollection<PlanSetModel> planSetList;

		public ObservableCollection<PlanSetModel> PlanSetList
        {
			get { return planSetList; }
			set { Set(ref planSetList,value); }
		}


		private int selectedPlanSet;

		public int SelectedPlanSet
        {
			get { return selectedPlanSet; }
			set { Set(ref selectedPlanSet,value); }
		}

        private ObservableCollection<ObjectID> objectIdList;

        public ObservableCollection<ObjectID> ObjectIdList
        {
            get { return objectIdList; }
            set { objectIdList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<NormalSetData> setDataList;

        public ObservableCollection<NormalSetData> SetDataList
        {
            get { return setDataList; }
            set { setDataList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<ConfigTableData> configTableDataList;

        public ObservableCollection<ConfigTableData> ConfigTableDataList
        {
            get { return configTableDataList; }
            set { configTableDataList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<PulseData> pulseDataList;

        public ObservableCollection<PulseData> PulseDataList
        {
            get { return pulseDataList; }
            set { pulseDataList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<AssociationData> associationDataList;

        public ObservableCollection<AssociationData> AssociationDataList
        {
            get { return associationDataList; }
            set { associationDataList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<HolidayData> holidayDataList;
        public ObservableCollection<HolidayData> HolidayDataList
        {
            get { return holidayDataList; }
            set { holidayDataList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<ConfigParamData> configParamDataList;
        public ObservableCollection<ConfigParamData> ConfigParamDataList
        {
            get { return configParamDataList; }
            set { configParamDataList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<GroupAddressData> groupAddressDataList;
        public ObservableCollection<GroupAddressData> GroupAddressDataList
        {
            get { return groupAddressDataList; }
            set { groupAddressDataList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<TimeZoneTableData> timeZoneTableDataList;
        public ObservableCollection<TimeZoneTableData> TimeZoneTableDataList
        {
            get { return timeZoneTableDataList; }
            set { timeZoneTableDataList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<PriceData> priceDataList;
        public ObservableCollection<PriceData> PriceDataList
        {
            get { return priceDataList; }
            set { priceDataList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<SettlementDayData> settlementDayDataList;
        public ObservableCollection<SettlementDayData> SettlementDayDataList
        {
            get { return settlementDayDataList; }
            set { settlementDayDataList = value; RaisePropertyChanged(); }
        }
        private ObservableCollection<FreezeAssociationData> freezeAssociationDataList;
        public ObservableCollection<FreezeAssociationData> FreezeAssociationDataList
        {
            get { return freezeAssociationDataList; }
            set { freezeAssociationDataList = value; RaisePropertyChanged(); }
        }



        private bool dataGridNormalVisibility;

        public bool DataGridNormalVisibility
        {
            get
            {

                return dataGridNormalVisibility;
            }
            set { dataGridNormalVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridConfigTableVisibility;

        public bool DataGridConfigTableVisibility
        {
            get
            {

                return dataGridConfigTableVisibility;
            }
            set { dataGridConfigTableVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridAssociationVisibility;

        public bool DataGridAssociationVisibility
        {
            get { return dataGridAssociationVisibility; }
            set { dataGridAssociationVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridPulseVisibility;

        public bool DataGridPulseVisibility
        {
            get { return dataGridPulseVisibility; }
            set { dataGridPulseVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridPositionVisibility;

        public bool DataGridPositionVisibility
        {
            get { return dataGridPositionVisibility; }
            set { dataGridPositionVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridHolidayVisibility;

        public bool DataGridHolidayVisibility
        {
            get { return dataGridHolidayVisibility; }
            set { dataGridHolidayVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridConfigParamVisibility;

        public bool DataGridConfigParamVisibility
        {
            get { return dataGridConfigParamVisibility; }
            set { dataGridConfigParamVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridGroupAddressVisibility;

        public bool DataGridGroupAddressVisibility
        {
            get { return dataGridGroupAddressVisibility; }
            set { dataGridGroupAddressVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridTimeZoneTableVisibility;

        public bool DataGridTimeZoneTableVisibility
        {
            get { return dataGridTimeZoneTableVisibility; }
            set { dataGridTimeZoneTableVisibility = value; RaisePropertyChanged(); }
        }
        private bool dataGridPriceVisibility;

        public bool DataGridPriceVisibility
        {
            get { return dataGridPriceVisibility; }
            set { dataGridPriceVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridSettlementDayVisibility;

        public bool DataGridSettlementDayVisibility
        {
            get { return dataGridSettlementDayVisibility; }
            set { dataGridSettlementDayVisibility = value; RaisePropertyChanged(); }
        }

        private bool dataGridFreezeAssociationVisibility;

        public bool DataGridFreezeAssociationVisibility
        {
            get { return dataGridFreezeAssociationVisibility; }
            set { dataGridFreezeAssociationVisibility = value; RaisePropertyChanged(); }
        }

        private bool connectVisibility;

        public bool ConnectVisibility
        {
            get { return connectVisibility; }
            set { connectVisibility = value; RaisePropertyChanged(); }
        }
        private bool btnReadVisibility;
        public bool BtnReadVisibility
        {
            get { return btnReadVisibility; }
            set { btnReadVisibility = value; RaisePropertyChanged(); }
        }


        private int selectedLOPosition;

        public int SelectedLOPosition
        {
            get { return selectedLOPosition; }
            set { selectedLOPosition = value; RaisePropertyChanged(); SavePosition(); }
        }
        private int selectedLAPosition;

        public int SelectedLAPosition
        {
            get { return selectedLAPosition; }
            set { selectedLAPosition = value; RaisePropertyChanged(); SavePosition(); }
        }
        private string lODegree;

        public string LODegree
        {
            get { if (string.IsNullOrEmpty(lODegree)) LODegree = "0"; return lODegree; }
            set { lODegree = value; RaisePropertyChanged(); SavePosition(); }
        }
        private string lADegree;

        public string LADegree
        {
            get { if (string.IsNullOrEmpty(lADegree)) LADegree = "0"; return lADegree; }
            set { lADegree = value; RaisePropertyChanged(); SavePosition(); }
        }

        private string lOMin;

        public string LOMin
        {
            get { if (string.IsNullOrEmpty(lOMin)) LOMin = "0"; return lOMin; }
            set { lOMin = value; RaisePropertyChanged(); SavePosition(); }
        }

        private string lAMin;

        public string LAMin
        {
            get { if (string.IsNullOrEmpty(lAMin)) LAMin = "0"; return lAMin; }
            set { lAMin = value; RaisePropertyChanged(); SavePosition(); }
        }

        private string lOSec;

        public string LOSec
        {
            get { if (string.IsNullOrEmpty(lOSec)) LOSec = "0"; return lOSec; }
            set { lOSec = value; RaisePropertyChanged(); SavePosition(); }
        }

        private string lASec;

        public string LASec
        {
            get { if (string.IsNullOrEmpty(lASec)) LASec = "0"; return lASec; }
            set { lASec = value; RaisePropertyChanged(); SavePosition(); }
        }

        private string high;

        public string High
        {
            get { if (string.IsNullOrEmpty(high)) High = "0"; return high; }
            set { high = value; RaisePropertyChanged(); SavePosition(); }
        }
        /// <summary>
        /// 切换设置参数表格
        /// </summary>
        /// <param name="zIndexName"></param>
        private void SetZIndex(string zIndexName)
        {
            DataGridNormalVisibility = false;
            DataGridConfigTableVisibility = false;
            DataGridPulseVisibility = false;
            DataGridAssociationVisibility = false;
            DataGridPositionVisibility = false;
            DataGridHolidayVisibility = false;
            DataGridConfigParamVisibility = false;
            DataGridGroupAddressVisibility = false;
            DataGridTimeZoneTableVisibility = false;
            DataGridPriceVisibility = false;
            DataGridSettlementDayVisibility = false;
            DataGridFreezeAssociationVisibility = false;
            ConnectVisibility = false;
            Messenger.Default.Send<bool>(false, "ConnectVisibility");
            switch (zIndexName)
            {
                case "DataGridNormalVisibility":
                    DataGridNormalVisibility = true;
                    break;
                case "DataGridConfigTableVisibility":
                    DataGridConfigTableVisibility = true;
                    break;
                case "DataGridPulseVisibility":
                    DataGridPulseVisibility = true;
                    break;
                case "DataGridAssociationVisibility":
                    DataGridAssociationVisibility = true;
                    break;
                case "DataGridPositionVisibility":
                    DataGridPositionVisibility = true;
                    break;
                case "DataGridHolidayVisibility":
                    DataGridHolidayVisibility = true;
                    break;
                case "DataGridConfigParamVisibility":
                    DataGridConfigParamVisibility = true;
                    break;
                case "DataGridGroupAddressVisibility":
                    DataGridGroupAddressVisibility = true;
                    break;
                case "DataGridTimeZoneTableVisibility":
                    DataGridTimeZoneTableVisibility = true;
                    break;
                case "DataGridPriceVisibility":
                    DataGridPriceVisibility = true;
                    break;
                case "DataGridSettlementDayVisibility":
                    DataGridSettlementDayVisibility = true;
                    break;
                case "DataGridFreezeAssociationVisibility":
                    DataGridFreezeAssociationVisibility = true;
                    break;
                case "ConnectVisibility":
                    ConnectVisibility = true;
                    //Messenger.Default.Send<bool>(true, "ConnectVisibility");
                    //CONNECTControlViewModel.action(true);
                    break;
                default:
                    break;
            }

        }

        private void SavePosition()
        {
            if (gWCmdMakeUpSelected == null)
            {
                return;
            }
            string value = "";
            value += SelectedLOPosition.ToString();
            value += ";";
            value += LODegree;
            value += ";";
            value += LOMin;
            value += ";";
            value += LOSec;
            value += ";";
            value += SelectedLAPosition.ToString();
            value += ";";
            value += LADegree;
            value += ";";
            value += LAMin;
            value += ";";
            value += LASec;
            value += ";";
            value += High;
            value += ";";
            gWCmdMakeUpSelected.DataValue = value;
            gWCmdMakeUpPlanService.Update(gWCmdMakeUpSelected);
        }
        /// <summary>
        /// 获取该OAD的所有设置项
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        private List<GWCmdMakeUp> getGWCmdMakeUpList(ObjectID objectID)
        {

            if (objectID == null)
            {
                return null;
            }

            return gWCmdMakeUpPlanService.Select(objectID);
        }
        /// <summary>
        /// 生成设置参数列表
        /// </summary>
        private void OIDataGridSelected()
        {
            if (SelectedPlanSet < 0)
            {
                SelectedPlanSet = 0;
            }
            if (ObjectIdList == null || ObjectIdList.Count <= 0)
            {
                return;
            }
            Console.WriteLine("=======================");
            var o = ObjectIdList[(int)SelectedPlanSet];
            var list = getGWCmdMakeUpList(o);
            string OADName = o.ObjectDes;
            if (OADName.Contains("总加配置表") || OADName.Contains("批量添加"))
            {

                List<ConfigTableData> configTableList = new List<ConfigTableData>();
                if (ConfigTableDataList != null)
                {
                    ConfigTableDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i += 3)
                        {
                            ConfigTableData configTableData = new ConfigTableData();
                            configTableData.TSA = strings[i];
                            configTableData.TotalFlag = strings[i + 1];
                            configTableData.OperFlag = strings[i + 2];
                            configTableList.Add(configTableData);
                        }
                        ConfigTableDataList = new ObservableCollection<ConfigTableData>(configTableList);
                    }
                    else
                    {
                        ConfigTableDataList = new ObservableCollection<ConfigTableData>();
                    }
                }

                SetZIndex("DataGridConfigTableVisibility");
            }
            else if (OADName.Contains("脉冲配置"))
            {
                List<PulseData> pulseList = new List<PulseData>();
                if (PulseDataList != null)
                {
                    PulseDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i += 3)
                        {
                            PulseData configTableData = new PulseData();
                            configTableData.OAD = strings[i];
                            configTableData.Enum = strings[i + 1];
                            configTableData.K = strings[i + 2];
                            pulseList.Add(configTableData);
                        }
                        PulseDataList = new ObservableCollection<PulseData>(pulseList);
                    }
                    else
                    {
                        PulseDataList = new ObservableCollection<PulseData>();
                    }
                }

                SetZIndex("DataGridPulseVisibility");
            }
            else if (OADName.Contains("事件-关联对象属性表") || OADName.Contains("恢复出厂参数"))
            {
                List<AssociationData> associationList = new List<AssociationData>();
                if (AssociationDataList != null)
                {
                    AssociationDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i++)
                        {
                            AssociationData associationData = new AssociationData();
                            associationData.OAD = strings[i];
                            associationList.Add(associationData);
                        }
                        AssociationDataList = new ObservableCollection<AssociationData>(associationList);
                    }
                    else
                    {
                        AssociationDataList = new ObservableCollection<AssociationData>();
                    }
                }

                SetZIndex("DataGridAssociationVisibility");
            }
            else if (OADName.Contains("地理位置"))
            {
                gWCmdMakeUpSelected = list[0];
                if (gWCmdMakeUpSelected != null)
                {
                    var strings = gWCmdMakeUpSelected.DataValue.Split(';');
                    SelectedLOPosition = int.Parse(strings[0]);
                    LODegree = strings[1];
                    LOMin = strings[2];
                    LOSec = strings[3];
                    SelectedLAPosition = int.Parse(strings[4]);
                    LADegree = strings[5];
                    LAMin = strings[6];
                    LASec = strings[7];
                    High = strings[8];
                }


                SetZIndex("DataGridPositionVisibility");
            }
            else if (OADName.Contains("公共假日表"))
            {
                List<HolidayData> holidayList = new List<HolidayData>();
                if (HolidayDataList != null)
                {
                    HolidayDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i += 2)
                        {
                            HolidayData holidayData = new HolidayData();
                            holidayData.DateTime = strings[i];
                            holidayData.Address = strings[i + 1];
                            holidayList.Add(holidayData);
                        }
                        HolidayDataList = new ObservableCollection<HolidayData>(holidayList);
                    }
                    else
                    {
                        HolidayDataList = new ObservableCollection<HolidayData>();
                    }
                }
                SetZIndex("DataGridHolidayVisibility");
            }
            else if (OADName.Contains("有功总电能量差动越限事件记录-配置参数"))
            {
                List<ConfigParamData> configParamList = new List<ConfigParamData>();
                if (HolidayDataList != null)
                {
                    HolidayDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i += 6)
                        {
                            ConfigParamData configParamData = new ConfigParamData();
                            configParamData.Id = strings[0];
                            configParamData.OIContrast = strings[1];
                            configParamData.OIRefer = strings[2];
                            configParamData.Flag = strings[3];
                            configParamData.DeviationReData = strings[4];
                            configParamData.DeviationAbData = strings[5];
                            configParamList.Add(configParamData);
                        }
                        ConfigParamDataList = new ObservableCollection<ConfigParamData>(configParamList);
                    }
                    else
                    {
                        ConfigParamDataList = new ObservableCollection<ConfigParamData>();
                    }
                }
                SetZIndex("DataGridConfigParamVisibility");
            }
            else if (OADName.Contains("组地址"))
            {
                List<GroupAddressData> groupAddressList = new List<GroupAddressData>();
                if (AssociationDataList != null)
                {
                    AssociationDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i++)
                        {
                            GroupAddressData groupAddressData = new GroupAddressData();
                            groupAddressData.Address = strings[i];
                            groupAddressList.Add(groupAddressData);
                        }
                        GroupAddressDataList = new ObservableCollection<GroupAddressData>(groupAddressList);
                    }
                    else
                    {
                        GroupAddressDataList = new ObservableCollection<GroupAddressData>();
                    }
                }

                SetZIndex("DataGridGroupAddressVisibility");
            }
            else if (OADName.Contains("当前套时区表") || OADName.Contains("备用套时区表"))
            {
                List<TimeZoneTableData> timeZoneTableList = new List<TimeZoneTableData>();
                if (PulseDataList != null)
                {
                    PulseDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i += 3)
                        {
                            TimeZoneTableData timeZoneTableData = new TimeZoneTableData();
                            timeZoneTableData.Month = strings[i];
                            timeZoneTableData.Day = strings[i + 1];
                            timeZoneTableData.Address = strings[i + 2];
                            timeZoneTableList.Add(timeZoneTableData);
                        }
                        TimeZoneTableDataList = new ObservableCollection<TimeZoneTableData>(timeZoneTableList);
                    }
                    else
                    {
                        TimeZoneTableDataList = new ObservableCollection<TimeZoneTableData>();
                    }
                }

                SetZIndex("DataGridTimeZoneTableVisibility");
            }
            else if (OADName.Contains("套费率电价"))
            {
                List<PriceData> priceList = new List<PriceData>();
                if (AssociationDataList != null)
                {
                    AssociationDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i++)
                        {
                            PriceData priceData = new PriceData();
                            priceData.Price = strings[i];
                            priceList.Add(priceData);
                        }
                        PriceDataList = new ObservableCollection<PriceData>(priceList);
                    }
                    else
                    {
                        PriceDataList = new ObservableCollection<PriceData>();
                    }
                }

                SetZIndex("DataGridPriceVisibility");
            }
            else if (OADName.Contains("结算日"))
            {
                List<SettlementDayData> settlementDayList = new List<SettlementDayData>();
                if (HolidayDataList != null)
                {
                    HolidayDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i += 2)
                        {
                            SettlementDayData s = new SettlementDayData();
                            s.Day = strings[i];
                            s.Hour = strings[i + 1];
                            settlementDayList.Add(s);
                        }
                        SettlementDayDataList = new ObservableCollection<SettlementDayData>(settlementDayList);
                    }
                    else
                    {
                        SettlementDayDataList = new ObservableCollection<SettlementDayData>();
                    }
                }
                SetZIndex("DataGridSettlementDayVisibility");
            }
            else if (OADName.Contains("冻结-关联对象属性表"))
            {
                List<FreezeAssociationData> freezeAssociationList = new List<FreezeAssociationData>();
                if (FreezeAssociationDataList != null)
                {
                    FreezeAssociationDataList.Clear();
                }
                foreach (var item in list)
                {
                    if (!string.IsNullOrEmpty(item.DataValue))
                    {
                        string[] strings = item.DataValue.Split(';');
                        for (int i = 0; i < strings.Length; i += 3)
                        {
                            FreezeAssociationData freezeAssociationData = new FreezeAssociationData();
                            freezeAssociationData.Cycle = strings[i];
                            freezeAssociationData.OAD = strings[i + 1];
                            freezeAssociationData.Deep = strings[i + 2];
                            freezeAssociationList.Add(freezeAssociationData);
                        }
                        FreezeAssociationDataList = new ObservableCollection<FreezeAssociationData>(freezeAssociationList);
                    }
                    else
                    {
                        FreezeAssociationDataList = new ObservableCollection<FreezeAssociationData>();
                    }
                }

                SetZIndex("DataGridFreezeAssociationVisibility");
            }
            else if (OADName.Contains("建立应用连接"))
            {

                SetZIndex("ConnectVisibility");
            }
            else if (list != null && list.Count > 0)
            {
                int AttributeID = (int)o.AttributeID;
                string OAD;
                if (o._ObjectID.ToString().Length == 8)
                {
                    OAD = o._ObjectID.ToString();
                }
                else
                {
                    OAD = o._ObjectID.ToString() + AttributeID.ToString("X2") + "00";
                }
                var query = SqlSugarHelper.Db.Queryable<GWCmdMakeUp>()
                 .LeftJoin<CJDataType>((g, c) => g.DataFormat == c.Datatypeid)//多个条件用&&
                 .Where(g => g.ObjectIdentifier.Equals(OAD)).Where(g => g.OpenrateType.Equals(o.ClientAPDU))
                 .Select((g, c) => new NormalSetData { SubItemName = g.SubItemName, DataValue = g.DataValue, Remark = g.Remark, Typename = c.Typename, Example = c.Example })
                 .ToList();
                SetDataList = new ObservableCollection<NormalSetData>(query);
                SetZIndex("DataGridNormalVisibility");
            }
            else
            {
                SetZIndex("EmptyZIndex");
            }
            gWCmdMakeUps = list;
        }
    }
}
